package dao

import (
	"fmt"
	redis2 "github.com/garyburd/redigo/redis"
	"github.com/jmoiron/sqlx"
	"my-crawler/concurrence/model"
	"my-crawler/concurrence/parser"
	"my-crawler/pkg"
	"time"
)

func SaveInRedis(db *redis2.Pool, item model.SaveItem) {
	fmt.Println("saver Item")
	//redis.SAdd("zhenai_user2", item.Name)
	pool := db.Get()
	_, err := pool.Do("SAdd", "zhenai_user2", item.Name)
	common.Check(err)
}

func SaveInMysql(Db *sqlx.DB, item interface{}) {
	//断言
	obj := item.(parser.Detail)
	//转换时间戳
	unix := time.Unix(obj.UpdateTime, 0)
	t := unix.Format("2006-01-02 03:04:05")
	r, err := Db.Exec("insert into crawler_articles(title, link, published_at,updated_at)values(?, ?,?, ?)",
		obj.Title, obj.Link, t, t)
	if err != nil {
		fmt.Println("exec failed, ", err)
		return
	}
	_, err = r.LastInsertId()
	if err != nil {
		fmt.Println("exec failed, ", err)
		return
	}

}
